<?php

/*
 * This file will perform the ajax for archiving an attachment.
 * The following tables will be inserted and/or updated with data :
 * Attachment
 * Log
 * 
 * Created by: Peter Agno Jr.
 * Date created: Octber 20, 2011
 * 
 * In parameters: attachmentId
 * Out parameters: archiveFlag
 */

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) { 
    include('../includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    $attachmentId = $_POST['attachmentId'];

    // Update Attachment table, status = 'Archived'
    $query =
        "
            UPDATE Attachment
            SET status = 'Inactive'
            WHERE attachmentId = '$attachmentId'
        ";
    
    mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

    // Get userId which will be the sender
    $userId = $_SESSION['loggedUserId'];
    
    // Get current date and time
    date_default_timezone_set('Asia/Singapore');
    $currentDateTime= date("Y-m-d H:i:s" , time());
    
    // Insert data in Log
    $query =
        "
            INSERT INTO Log (logId, documentId, userId, stepId, logDateTime, activity) VALUES
                ('', '$attachmentId', '$userId', NULL, '$currentDateTime', 'Archived');
        ";

    mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    $feed['archiveFlag'] = "Success";
    $feed['msg'] = $attachmentId." was successfully archived.";
    
    echo json_encode($feed);
//    mysql_free_result($result); uncomment if there will be a result
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
